using System;
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;
using System.Windows.Forms;

// Book Lookup Plugin for Windows Live Writer
// Utilizes the Amazon.com ECommerce Web Service to retrieve book information and insert into a blog post.
// Created by: Justin Braun - http://www.justinbraun.com/
// Created: Saturday, September 16, 2006

namespace BookLookupPlugin
{
    [WriterPluginAttribute
      ("94260862-e361-4bbc-b53b-24166b481c4b",
           "Book Lookup Plugin",
            ImagePath = "Images.book.png",
            HasEditableOptions = false,
            PublisherUrl = "http://www.justinbraun.com/",
            Description = "Insert book information from the Amazon Web Service into your blog post.")]

    [InsertableContentSourceAttribute("Book Information")]
    public class BookLookupPlugin : ContentSource
    {
        public override DialogResult CreateContent(System.Windows.Forms.IWin32Window dialogOwner, ref string newContent)
        {
            
            try
            {
                // Get Search Results
                ListViewItem item = this.GetContent();

                if (item != null)
                {
                    // if the listviewitem contains data, then format it
                    newContent = this.FormatOutput(item);
                }
                return DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return DialogResult.OK;
            }
        }

        private string FormatOutput(ListViewItem item)
        {
            // Used to format the ListViewItem data for final HTML output
            StringBuilder html = new StringBuilder();
            html.Append("<p><table border='0'>");
            html.Append("<tr><td valign='top'><img src='" + item.SubItems[3].Text + "' border='1'></td>");
            html.Append("<td valign='top'><b>" + item.SubItems[0].Text + "</b><br>");
            html.Append("by " + item.SubItems[1].Text + "<br><br>");
            html.Append("<a href='" + item.SubItems[2].Text + "'>Read more about this title...</a></td></tr>");
            html.Append("</table></p>");
            return html.ToString();

        }

        private ListViewItem GetContent()
        {
            // Show Search Form
            searchForm search = new searchForm();

            ListViewItem item = null;
            search.ShowDialog();

            if (search.ResultListViewItem != null)
            {
                item = search.ResultListViewItem;
                //MessageBox.Show(item.Text + " by " + item.SubItems[1].Text);
                return item;
            }
            else if (search.DialogResult != DialogResult.Cancel)
            {
                throw new Exception("Unable to retrieve data.  Please try again later.");
            }
            else
            {
                return item;
            }
        }
    }
}
